<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');
$timestamp    = strftime("%Y-%m-%d %H:%M:%S %Y");
$currmonth    = ""; 
$partyId      = (isset($_POST['party'])) ? $_POST['party'] : 0;
$isEdit       = 0;
$crfine       = "";
$msg          = isset($_GET['msg']) ? $$_GET['msg'] : "";
$queryField   = "";
$queryValue   = "";
$billDate     = "";
$bulliondate  = "";
$party        = array();
$bullionArray = array();
$totalFine    = (isset($_POST['totFine']) && $_POST['totFine'] > 0) ? $_POST['totFine'] : 0;
$totalAmount  = (isset($_POST['totAmount']) && $_POST['totAmount'] > 0) ? $_POST['totAmount'] : 0;
$removePrev   = (isset($_POST['removePrev'])) ? 1 : 0;
$bullionmasterid = "";
if(isset($_POST['billBookAddBtn']))
{
  if($_POST['isEdit'] = '1')
  {
    $updateMasterQuery = "UPDATE bullionmaster SET partyId = ".$_POST['party'].", bulliondate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."'
   	                       WHERE bullionmasterid = ".$_POST['bullionmasterid'];
   	$updateMasterQueryRes = mysql_query($updateMasterQuery);
  	
  	$deleteMasterQuery = "DELETE FROM bulliondetail 
  	                      WHERE bullionmasterid = ".$_POST['bullionmasterid'];
  	$deleteMasterQueryRes = mysql_Query($deleteMasterQuery);
    
    $bullionInsert = "INSERT INTO  bullionmaster (".$queryField."partyId, bulliondate)
                      VALUES (".$queryValue."".$_POST['party'].",'".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."')";
    $bullionInsertRes = mysql_query($bullionInsert);
    $bullionmasterid  = mysql_insert_id();
    
    $loopCount = 0;
    while($loopCount < count($_POST['particulars']))
    {
      $kg           = ($_POST['kg'][$loopCount] != '') ? $_POST['kg'][$loopCount] : 0;
      $bhav         = ($_POST['bhav'][$loopCount] != '') ? $_POST['bhav'][$loopCount] : 0;
      $cramount     = ($_POST['cramount'][$loopCount] != '') ? $_POST['cramount'][$loopCount] : 0;
      $crfine       = ($_POST['crfine'][$loopCount] != '') ? $_POST['crfine'][$loopCount] : 0;
      $dramount     = ($_POST['dramount'][$loopCount] != '') ? $_POST['dramount'][$loopCount] : 0;
      
        if($_POST['particulars'][$loopCount] != "" || $_POST['kg'][$loopCount] != '' || $_POST['bhav'][$loopCount] != '' || $_POST['cramount'][$loopCount] != '' || $_POST['crfine'][$loopCount] != '' || $_POST['dramount'][$loopCount] != '')
      {
        $billBookDetail = "INSERT INTO  bulliondetail (bullionmasterid,particulars, kg, bhav, cramount, crfine, dramount)
                           VALUES (".$bullionmasterid.",'".addslashes($_POST['particulars'][$loopCount])."',".$kg.",".$bhav.",".$cramount.",".$crfine.",".$dramount.")";
        $billBookDetailRes = mysql_query($billBookDetail);
      }
      $loopCount++;
    }
   }
   else
   {
//     {
//       $bullionInsert = "INSERT INTO  bullionmaster (".$queryField."partyId, bulliondate)
//                         VALUES (".$queryValue."".$_POST['party'].",'".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."')";
//       $bullionInsertRes = mysql_query($bullionInsert);
//       $bullionmasterid  = mysql_insert_id();
//     }
//    
//     $loopCount = 0;
//     while($loopCount < count($_POST['particulars']))
//     {
//       $kg           = ($_POST['kg'][$loopCount] != '') ? $_POST['kg'][$loopCount] : 0;
//       $bhav         = ($_POST['bhav'][$loopCount] != '') ? $_POST['bhav'][$loopCount] : 0;
//       $cramount     = ($_POST['cramount'][$loopCount] != '') ? $_POST['cramount'][$loopCount] : 0;
//       $crfine       = ($_POST['crfine'][$loopCount] != '') ? $_POST['crfine'][$loopCount] : 0;
//       $dramount     = ($_POST['dramount'][$loopCount] != '') ? $_POST['dramount'][$loopCount] : 0;
//      
//       if($_POST['particulars'][$loopCount] != "" || $_POST['kg'][$loopCount] != '' || $_POST['bhav'][$loopCount] != '' || $_POST['cramount'][$loopCount] != '' || $_POST['crfine'][$loopCount] != '' || $_POST['dramount'][$loopCount] != '')
//      {
//        $billBookDetail = "INSERT INTO  bulliondetail (bullionmasterid,particulars, kg, bhav, cramount, crfine, dramount)
//                           VALUES (".$bullionmasterid.",'".addslashes($_POST['particulars'][$loopCount])."',".$kg.",".$bhav.",".$cramount.",".$crfine.",".$dramount.")";
//        $billBookDetailRes = mysql_query($billBookDetail);
//      }
//      $loopCount++;
//     }  
    }
//   	$loopCount = 0;
//   	if(isset($_GET['bullionmasterid']) && ($_GET['bullionmasterid']))
//   	{
//   		$particulars  = ($_POST['particulars'][$loopCount] != '') ? $_POST['particulars'][$loopCount] : 0;
//   		$kg           = ($_POST['kg'][$loopCount] != '') ? $_POST['kg'][$loopCount] : 0;
//      $bhav         = ($_POST['bhav'][$loopCount] != '') ? $_POST['bhav'][$loopCount] : 0;
//      $cramount     = ($_POST['cramount'][$loopCount] != '') ? $_POST['cramount'][$loopCount] : 0;
//      $crfine       = ($_POST['crfine'][$loopCount] != '') ? $_POST['crfine'][$loopCount] : 0;
//      $dramount     = ($_POST['dramount'][$loopCount] != '') ? $_POST['dramount'][$loopCount] : 0;
//   	  $updateMasterQuery = "UPDATE bullionmaster SET partyId = ".$_POST['party'].", bulliondate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."'
//   	                         WHERE bullionmasterid = ".$_POST['bullionmasterid'];
//   	  $updateMasterQueryRes = mysql_query($updateMasterQuery);
//   	  
//   	  $updateDetailQuery = "UPDATE bulliondetail SET particulars =".$particulars.",kg = ".$kg.",bhav = ".$bhav.",
//   	                                                 cramount = ".$cramount.",crfine = ".$crfine.",dramount = ".$dramount."
//   	                         WHERE bullionmasterid = ".$_POST['bullionmasterid'];
//      $updateDetailQueryRes = mysql_query($updateDetailQuery);
//      header("location:bullionDetail.php"); 
//   	                                                
//    }
      header("location:bullionDetail.php?partyId=".$partyId);
   }

//  if($_POST['isEdit'] == 1 && $billAlreadyExist != 1) 
//  header("location:bullionEntry.php"); 

if(isset($_GET['bullionmasterid']) && ($_GET['bullionmasterid']))
{
	$selectMasterQuery = "SELECT bullionmasterid,partyId,DATE_FORMAT(bullionDate,'%Y-%m-%d') AS bullionMasterDate
	                        FROM bullionmaster 
	                       WHERE bullionmasterid = ".$_GET['bullionmasterid'];
	$selectMasterQueryRes = mysql_query($selectMasterQuery);
  $masterRow = mysql_fetch_array($selectMasterQueryRes);
  $partyId     = $masterRow['partyId'];
 	$bullionDate = $masterRow['bullionMasterDate'];
 	
 	$selectDetailQuery = "SELECT bullionmasterid,particulars,kg,bhav,cramount,crfine,dramount
 	                        FROM bulliondetail
 	                       WHERE bullionmasterId = ".$_GET['bullionmasterid'];
 	$selectDetailQueryRes = mysql_Query($selectDetailQuery);
 	$detailRowCount = 0;
 	while($detailRow = mysql_fetch_array($selectDetailQueryRes))
 	{
    $bullionArray[$detailRowCount]['particulars'] = $detailRow['particulars'];
    $bullionArray[$detailRowCount]['kg']          = $detailRow['kg'];
    $bullionArray[$detailRowCount]['bhav']        = $detailRow['bhav'];
    $bullionArray[$detailRowCount]['cramount']    = $detailRow['cramount'];
    $bullionArray[$detailRowCount]['crfine']      = $detailRow['crfine'];
    $bullionArray[$detailRowCount]['dramount']    = $detailRow['dramount'];
    $detailRowCount++;
  }
 	               
}
	if(isset($_GET['bullionmasterid']) && $_GET['bullionmasterid'] > 0)
{
  $isEdit = 1;
  
  $selectParty = "SELECT bullionmasterid,partyId,DATE_FORMAT(bullionDate,'%Y-%m-%d') AS bullionMasterDate
                    FROM bullionmaster
                   WHERE bullionmasterid = ".$_GET['bullionmasterid'];
  $selectPartyRes = mysql_query($selectParty);
  
  $row = mysql_fetch_assoc($selectPartyRes);
  
  $partyId         = $row['partyId'];
  $bulliondate     = $row['bullionMasterDate'];
  $bullionmasterid = $row['bullionmasterid'];
}

// This for select party : Start
$selectParty = "SELECT partyId,bullionPartyName 
                  FROM bullionparty
                 ORDER BY bullionPartyName DESC";
$selectPartyRes = mysql_query($selectParty);
$a = 0;
$party['partyId'][$a]          = 0;
$party['bullionPartyName'][$a] = "Select";
$a++;
while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
  $party['partyId'][$a]          = $partyRow['partyId'];
  $party['bullionPartyName'][$a] = $partyRow['bullionPartyName'];
  $a++;
}
// This for select party : End

$smarty = new smarty;
require("bottom.php");

$smarty->assign("l_partySelect",$l_partySelect);
$smarty->assign("l_bulliondate",$l_bulliondate);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_kg",$l_kg);
$smarty->assign("l_bhav",$l_bhav);
$smarty->assign("l_cramount",$l_cramount);
$smarty->assign("l_crfine",$l_crfine);
$smarty->assign("l_dramount",$l_dramount);
$smarty->assign("l_add",$l_add);
$smarty->assign("l_remove",$l_remove);
$smarty->assign("l_addBillBook",$l_addBillBook);
$smarty->assign("l_billBook",$l_billBook);
$smarty->assign("l_total",$l_total);
$smarty->assign("l_billBookDetailId",$l_billBookDetailId);
$smarty->assign("l_save",$l_save);

$smarty->assign("party",$party);
$smarty->assign("msg",$msg);
$smarty->assign("isEdit",$isEdit);
$smarty->assign("partyId",$partyId);
$smarty->assign("billDate",$billDate);
$smarty->assign("bulliondate",$bulliondate);
$smarty->assign("bullionArray",$bullionArray);
$smarty->assign("bullionmasterid",$bullionmasterid);
$smarty->display("./bullionEntry.tpl");
?>